function ajax(url) {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest();
    xhr.onload = function () {};
    xhr.open();
    xhr.send();
  });
}

function timer(time) {
  return new Promise((resolve, reject) => {
    setTimeout(function () {
      reject('超时了');
    }, time);
  });
}

Promise.race([ajax('./url'), timer(3000)])
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });
